﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using VehicleManagement.Models;
using VehicleManagement.Codes;
using System.Collections;
using VehicleManagement.Controllers;

namespace VehicleManagement.ControllersServices
{
    public class ApplyServices
    {
        /// <summary>
        /// 查找所有申请表，负责人权限
        /// </summary>
        /// <returns>申请列表</returns>
        //public IList<Apply> GetAllApplys(string companyName)
        //{
        //    IList<Apply> lstApply = SqlMapperManager.Instance.Get().QueryForList<Apply>("SELECT_ALL_APPLY", companyName);
        //    return lstApply;
        //}


        //public IList<Apply> SearchAllApplys(Hashtable ht)
        //{
        //    IList<Apply> lstApply = new List<Apply> { };
        //    lstApply = SqlMapperManager.Instance.Get().QueryForList<Apply>("SEARCH_APPLY", ht);
        //    return lstApply;

        //    //if (Convert.ToInt32(ht["applyStateForSearch"]) != 3)
        //    //{
        //    //    lstApply = SqlMapperManager.Instance.Get().QueryForList<Apply>("SEARCH_APPLY_HASSTATE", ht);
        //    //    return lstApply;
        //    //}
        //    //else
        //    //{
        //    //    lstApply = SqlMapperManager.Instance.Get().QueryForList<Apply>("SEARCH_APPLY_NOSTATE", ht);
        //    //    return lstApply;
        //    //}
        //}

        public IList<Apply> SearchAllApplys(Hashtable ht,int skip,int max)
        {
            IList<Apply> lstApply = new List<Apply> { };
            lstApply = SqlMapperManager.Instance.Get().QueryForList<Apply>("SEARCH_APPLY", ht,skip,max);
            return lstApply;

            //if (Convert.ToInt32(ht["applyStateForSearch"]) != 3)
            //{
            //    lstApply = SqlMapperManager.Instance.Get().QueryForList<Apply>("SEARCH_APPLY_HASSTATE", ht);
            //    return lstApply;
            //}
            //else
            //{
            //    lstApply = SqlMapperManager.Instance.Get().QueryForList<Apply>("SEARCH_APPLY_NOSTATE", ht);
            //    return lstApply;
            //}
        }

        public int CountAllApplys(Hashtable ht)
        {
            IList<Apply> lstApply = new List<Apply> { };
            lstApply = SqlMapperManager.Instance.Get().QueryForList<Apply>("SEARCH_APPLY", ht);
            return lstApply.Count;
        }
        /// <summary>
        /// 查找某个申请人所有申请表，负责人和申请人权限
        /// </summary>
        /// <param name="id">申请人ID</param>
        /// <returns>申请列表</returns>
        public IList<Apply> GetApplysByApplicantID(Hashtable ht)
        {
            IList<Apply> lstApply = SqlMapperManager.Instance.Get().QueryForList<Apply>("SELECT_APPLY_BY_APPLICANTID", ht);
            return lstApply;
        }

        /// <summary>
        /// 根据编号查找某个申请表
        /// </summary>
        /// <param name="id">申请表ID</param>
        /// <returns>申请表信息对象</returns>
        public Apply GetApplyByApplyID(int id)
        {
            IList<Apply> lstApply = SqlMapperManager.Instance.Get().QueryForList<Apply>("SELECT_APPLY_BY_APPLYID", id);
            Apply apply = lstApply.Where(u => u.ApplyID == id).First();
            return apply;
        }

        /// <summary>
        /// 通过申请表ID获取申请人ID
        /// </summary>
        /// <param name="applyID">申请表ID</param>
        /// <returns>申请人ID</returns>
        public int getApplicantIDByApplyID(int id)
        {
            int applicantID = Convert.ToInt32(SqlMapperManager.Instance.Get().QueryForObject("SELECT_APPLICANTID_BY_APPLYID", id)); ;
            return applicantID;
        }

        /// <summary>
        /// 增加一个申请并自动创建一个空的审批
        /// </summary>
        /// <param name="id">申请表信息对象</param>
        /// <returns>所增加条目的主键</returns>
        public object CreateApply(Apply apply)
        {
            int lastApplyID;
            lock (apply)
            {
                SqlMapperManager.Instance.Get().Insert("INSERT_APPLY", apply);
                lastApplyID = Convert.ToInt32(SqlMapperManager.Instance.Get().QueryForObject("SELECT_LAST_APPLYID", null));
                SqlMapperManager.Instance.Get().Insert("INSERT_NEW_APPROVAL", lastApplyID);
            }
            return lastApplyID;
        }

        /// <summary>
        /// 修改一个申请
        /// </summary>
        /// <param name="id">申请表信息对象</param>
        /// <returns>修改列数</returns>
        public int EditApply(Apply apply)
        {
            return SqlMapperManager.Instance.Get().Update("UPDATE_APPLY", apply);
        }

        /// <summary>
        /// 删除一个申请，并且删除相关审批信息（不通过的才能删除）
        /// </summary>
        /// <param name="id">申请ID</param>
        /// <returns>删除列数</returns>
        public int DeleteApply(int id)
        {
            SqlMapperManager.Instance.Get().Delete("DELETE_APPROVAL_BY_APPLYID", id);
            return SqlMapperManager.Instance.Get().Delete("DELETE_APPLY", id);
        }
    }
}